﻿#region 命名空间

using System;
using System.Net;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.Web.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

#endregion

namespace Sharp.Web.Controllers
{
    [AutomaticTemplate]
    public class UserGroupController : Controller
    {
        // GET: Manage/UserGroup
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                Model = new UserGroupIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(StoreRequestParameters parameters)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var pageInfo = proxy.Channel.GetUserGroupPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }

        // GET: Manage/UserGroup/Details
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserGroupDTO userGroupDto = null;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                userGroupDto = proxy.Channel.GetUserGroupByKey(id.Value);
            }
            if (userGroupDto == null)
            {
                return HttpNotFound();
            }

            return new PartialViewResult { ViewName = "Details", Model = userGroupDto.To<UserGroupModel>() };
        }

        // GET: Manage/UserGroup/Create
        public ActionResult Create()
        {
            return new PartialViewResult { ViewName = "Edit", Model = new UserGroupModel() };
        }

        // GET: Manage/UserGroup/Edit
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserGroupDTO userGroupDto = null;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                userGroupDto = proxy.Channel.GetUserGroupByKey(id.Value);
            }
            if (userGroupDto == null)
            {
                return HttpNotFound();
            }
            return new PartialViewResult { ViewName = "Edit", Model = userGroupDto.To<UserGroupModel>() };
        }

        [HttpPost]
        public ActionResult Save(UserGroupModel userGroup)
        {
            using (var proxy = new ServiceProxy<IUserService>())
            {
                var userGroupDto = userGroup.To<UserGroupDTO>();
                if (userGroupDto.Id.IsEmpty())
                {
                    proxy.Channel.CreateUserGroup(userGroupDto);
                }
                else
                {
                    proxy.Channel.UpdateUserGroup(userGroupDto);
                }
            }

            return this.FormPanel();
        }

        [HttpPost]
        public JsonResult Delete(Guid[] ids)
        {
            var res = new JsonResult();
            using (var proxy = new ServiceProxy<IUserService>())
            {
                res.Data = proxy.Channel.DeleteUserGroups(ids);
            }

            return res;
        }

        /// <summary>
        /// 检查用户组名称是否被占用
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public JsonResult CheckTitle(string title)
        {
            bool valid;
            using (var proxy = new ServiceProxy<IUserService>())
            {
                valid = !proxy.Channel.CheckUserGroupName(title);
            }
            return Json(new { valid = true }, JsonRequestBehavior.AllowGet);
        }
    }
}